# -*- coding: utf-8 -*-
"""
视觉标准 - 统一的设计规范
参考成熟项目实现
"""

from enum import Enum
from typing import Dict, Any, Tuple


class Theme(Enum):
    """主题枚举"""
    DARK = "dark"  # 深色主题
    LIGHT = "light"  # 浅色主题
    ACADEMIC = "academic"  # 学术主题
    PRESENTATION = "presentation"  # 演示主题


# 字体配置
FONT_CONFIG = {
    "primary_font": "Microsoft YaHei",  # 主要字体
    "code_font": "Consolas",  # 代码字体
    "math_font": "KaTeX",  # 数学公式字体
    "sizes": {
        "title": 48,
        "subtitle": 36,
        "content": 32,
        "caption": 24,
        "small": 20,
        "code": 28,
        "formula": 40,
    },
}

# 颜色调色板
COLOR_PALETTE = {
    Theme.DARK: {
        "primary": "#4A90E2",  # 主色
        "secondary": "#7B68EE",  # 副色
        "accent": "#F5A623",  # 强调色
        "success": "#7ED321",  # 成功色
        "warning": "#F8E71C",  # 警告色
        "error": "#D0021B",  # 错误色
        "text": "#FFFFFF",  # 文本色
        "text_secondary": "#B0B0B0",  # 次要文本
        "background": "#1E1E1E",  # 背景色
        "surface": "#2D2D2D",  # 表面色
        "border": "#404040",  # 边框色
    },
    Theme.LIGHT: {
        "primary": "#2196F3",
        "secondary": "#673AB7",
        "accent": "#FF9800",
        "success": "#4CAF50",
        "warning": "#FFC107",
        "error": "#F44336",
        "text": "#212121",
        "text_secondary": "#757575",
        "background": "#FAFAFA",
        "surface": "#FFFFFF",
        "border": "#E0E0E0",
    },
    Theme.ACADEMIC: {
        "primary": "#2E86AB",
        "secondary": "#A23B72",
        "accent": "#F18F01",
        "success": "#C73E1D",
        "warning": "#FFE66D",
        "error": "#D62828",
        "text": "#2C3E50",
        "text_secondary": "#7F8C8D",
        "background": "#F8F9FA",
        "surface": "#FFFFFF",
        "border": "#DEE2E6",
    },
    Theme.PRESENTATION: {
        "primary": "#3F51B5",
        "secondary": "#9C27B0",
        "accent": "#FF5722",
        "success": "#4CAF50",
        "warning": "#FF9800",
        "error": "#E91E63",
        "text": "#212121",
        "text_secondary": "#757575",
        "background": "#000000",
        "surface": "#1A1A1A",
        "border": "#424242",
    },
}

# 布局常量
LAYOUT_CONSTANTS = {
    "safe_margin_x": 0.5,  # X轴安全边距
    "safe_margin_y": 0.4,  # Y轴安全边距
    "title_y": 3.2,  # 标题Y位置
    "content_top": 2.5,  # 内容顶部
    "content_bottom": -2.5,  # 内容底部
    "left_margin": -6.0,  # 左边距
    "right_margin": 6.0,  # 右边距
    "spacing_title": 0.5,  # 标题间距
    "spacing_content": 0.3,  # 内容间距
    "max_text_width": 10.0,  # 最大文本宽度
}

# 动画参数
ANIMATION_CONFIG = {
    "default_duration": 0.8,  # 默认动画时长
    "fade_duration": 0.5,  # 淡入淡出时长
    "write_duration": 1.2,  # 写入动画时长
    "wait_buffer": 0.3,  # 等待缓冲
    "transition_duration": 0.6,  # 过渡动画时长
    "ease_in_out": "easeInOutCubic",  # 缓动函数
}

# 质量配置
QUALITY_CONFIG = {
    "preview": {
        "resolution": (854, 480),
        "fps": 15,
        "anti_aliasing": False,
    },
    "standard": {
        "resolution": (1280, 720),
        "fps": 30,
        "anti_aliasing": True,
    },
    "high": {
        "resolution": (1920, 1080),
        "fps": 30,
        "anti_aliasing": True,
    },
    "ultra": {
        "resolution": (1920, 1080),
        "fps": 60,
        "anti_aliasing": True,
    },
}


class VisualStandards:
    """视觉标准管理器"""

    def __init__(self, theme):
        """
        初始化视觉标准

        Args:
            theme: 主题（可以是Theme枚举或字符串）
        """
        # 解析主题
        if isinstance(theme, str):
            self.theme = Theme(theme)
        elif isinstance(theme, Theme):
            self.theme = theme
        else:
            self.theme = Theme.DARK

        self.colors = COLOR_PALETTE[self.theme]
        self.layout = LAYOUT_CONSTANTS.copy()
        self.fonts = FONT_CONFIG.copy()
        self.animation = ANIMATION_CONFIG.copy()
        self.quality = QUALITY_CONFIG["standard"].copy()

    def get_color(self, color_name: str) -> str:
        """
        获取颜色

        Args:
            color_name: 颜色名称

        Returns:
            颜色值
        """
        return self.colors.get(color_name, self.colors["text"])

    def get_font_size(self, size_type: str) -> int:
        """
        获取字体大小

        Args:
            size_type: 大小类型

        Returns:
            字体大小
        """
        return self.fonts["sizes"].get(size_type, 32)

    def get_position(self, position_type: str) -> float:
        """
        获取位置

        Args:
            position_type: 位置类型

        Returns:
            位置坐标
        """
        return self.layout.get(position_type, 0.0)

    def set_quality(self, quality: str) -> None:
        """
        设置质量

        Args:
            quality: 质量等级
        """
        if quality in QUALITY_CONFIG:
            self.quality = QUALITY_CONFIG[quality].copy()

    def get_animation_config(self, animation_type: str) -> float:
        """
        获取动画配置

        Args:
            animation_type: 动画类型

        Returns:
            动画时长
        """
        return self.animation.get(animation_type, 0.8)

    def create_style_dict(self, element_type: str) -> Dict[str, Any]:
        """
        创建元素样式字典

        Args:
            element_type: 元素类型

        Returns:
            样式字典
        """
        if element_type == "title":
            return {
                "font_size": self.get_font_size("title"),
                "color": self.get_color("primary"),
                "font": self.fonts["primary_font"],
            }
        elif element_type == "subtitle":
            return {
                "font_size": self.get_font_size("subtitle"),
                "color": self.get_color("text"),
                "font": self.fonts["primary_font"],
            }
        elif element_type == "content":
            return {
                "font_size": self.get_font_size("content"),
                "color": self.get_color("text"),
                "font": self.fonts["primary_font"],
            }
        elif element_type == "caption":
            return {
                "font_size": self.get_font_size("caption"),
                "color": self.get_color("text_secondary"),
                "font": self.fonts["primary_font"],
            }
        elif element_type == "code":
            return {
                "font_size": self.get_font_size("code"),
                "color": self.get_color("accent"),
                "font": self.fonts["code_font"],
            }
        elif element_type == "math":
            return {
                "font_size": self.get_font_size("formula"),
                "color": self.get_color("secondary"),
            }
        else:
            return {
                "font_size": self.get_font_size("content"),
                "color": self.get_color("text"),
            }

    def validate_theme(self) -> Tuple[bool, list]:
        """
        验证主题配置

        Returns:
            (是否有效, 错误列表)
        """
        errors = []

        # 检查必需的颜色
        required_colors = ["primary", "text", "background"]
        for color in required_colors:
            if color not in self.colors:
                errors.append(f"Missing required color: {color}")

        # 检查字体大小
        required_sizes = ["title", "content"]
        for size in required_sizes:
            if size not in self.fonts["sizes"]:
                errors.append(f"Missing required font size: {size}")

        # 检查布局常量
        required_layout = ["title_y", "content_top"]
        for layout in required_layout:
            if layout not in self.layout:
                errors.append(f"Missing required layout constant: {layout}")

        return len(errors) == 0, errors

    def get_theme_info(self) -> Dict[str, Any]:
        """
        获取主题信息

        Returns:
            主题信息字典
        """
        return {
            "theme": self.theme.value,
            "color_count": len(self.colors),
            "font_count": len(self.fonts),
            "layout_count": len(self.layout),
        }
